﻿namespace AgileBoard.Infrastructure.Data.Repositories
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Domain;
    using Domain.DataInterfaces;
    using NHibernate;
    using SharpArch.Data.NHibernate;

    public class StoryRepository : Repository<Story>, IStoryRepository
    {
        public IList<Story> FindByFeature(string featureId)
        {
            return Session.CreateQuery("from Story where FeatureID = :featureId")
                .SetParameter("featureId", featureId)
                .List<Story>();
        }

        public IList<Story> FindByIteration(string iterationId)
        {
            return Session.CreateQuery("from Story where IterationID = :iterationId")
                .SetParameter("iterationId", iterationId)
                .List<Story>();
        }

        public IList<Story> FindByProject(Guid ProjectID)
        {
            IQuery query =
                Session.CreateQuery(
                    "from Story S,Iteration I, Release R, Project P where S.IterationID = I.IterationID and I.ReleaseID = R.ReleaseID and R.ProjectID = P.ID and P.ID='" +
                    ProjectID + "'");

            IList items = query.List();
            var stories = new List<Story>();

            foreach (object[] item in items)
            {
                var story = item[0] as Story;
                if (story != null)
                    stories.Add(story);
            }

            return stories;
        }
    }
}